package com.example.administrator.newsapp;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.newsapp.pojo.Favorite;

import java.util.List;

import javax.security.auth.login.LoginException;

/**
 * ClassName:
 * Description:
 */

public class ShowFavoriteAdapter extends BaseAdapter {
    private List<Favorite> mFavorites;
    private Context mContext;

    public ShowFavoriteAdapter(List<Favorite> favorites, Context context) {
        mFavorites = favorites;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mFavorites.size();
    }

    @Override
    public Object getItem(int position) {
        return mFavorites.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.favorite_list_item,parent,false);
            ViewHolder holder = new ViewHolder();
            holder.favImage =  convertView.findViewById(R.id.fav_image);
            holder.favText =  convertView.findViewById(R.id.fav_text);
            holder.favLove =  convertView.findViewById(R.id.fav_love);

            holder.favLove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemDelFavoriteListener.onDelFavoriteClick(position);

                }
            });
            //给View添加一个格外的数据，以后可以用getTag()将这个数据取出来。
            convertView.setTag(holder);
        }
        //设配器连接数据
        ViewHolder holder = (ViewHolder) convertView.getTag();
        Favorite f = mFavorites.get(position);
        Glide.with(mContext).load(f.getNew_image()).into(holder.favImage);
        holder.favText.setText(f.getNew_title());
        return convertView;
    }

    class ViewHolder{
        private ImageView favImage;
        private TextView favText;
        private ImageView favLove;
    }

    public interface onItemDelFavoriteListener{
        void onDelFavoriteClick(int i);
    }

    private onItemDelFavoriteListener mOnItemDelFavoriteListener;

    public void setOnItemDelFavoriteClickListener(onItemDelFavoriteListener mOnItemDelFavoriteListener) {
        this.mOnItemDelFavoriteListener = mOnItemDelFavoriteListener;
    }
}
